package com.hnkywl.sts.dao.djgl;

import com.hnkywl.sts.entity.djgl.Jydd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by VanHeiSing on 2015/12/15.
 */
@Repository
public class JyddDao extends SimpleHibernateTemplate<Jydd, Long> {

    /**
     * 验证订单唯一性
     *
     * @param ddhth
     * @return
     */
    public Jydd checkOrderSn(String ddhth) {
        List<Jydd> list = find("from Jydd j where j.order_sn =?", ddhth);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Page<Jydd> findJyddjList(String htbm, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jydd> page = new Page<Jydd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (htbm != null && !htbm.isEmpty()) {
            condition.append(" order_sn like ? ");
            param.add("%" + htbm.trim() + "%");
        }

        //==============记录数量===============
        hql = "select count(*) from Jydd";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jydd order by add_time desc";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Jydd> getAllJydds() {
        String hql = "from Jydd";
        List<Jydd> list = createQuery(hql).list();
        return list;
    }

}
